package accesoRepositorio;

import modelo.gestionCarta.*;
import modelo.gestionFacturacion.Balance;
import modelo.gestionFacturacion.LineaBalance;
import interfacesRemotas.*;
import java.rmi.RemoteException;
import java.rmi.server.UnicastRemoteObject;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import com.mysql.jdbc.ResultSet;

/**
 * @version 2.0
 * @created 21-mar-2007 23:14:35
 * @author Luis Miguel Merino Bonilla, Carmen Pastor Morales
 */
public class AccesoBalance extends UnicastRemoteObject implements InterfazBalance
{
	/**
	 * 
	 */
	private static final long serialVersionUID = 1512529039455279849L;
	public Seccion m_Seccion;
	public Carta m_Carta;
	
    public AccesoBalance() throws RemoteException
    {
    	super();
	}

	public void finalize() throws Throwable {

	}

	public boolean actualizarLineaBalance(int idLineaBalance, int precio) throws RemoteException {
		// TODO Auto-generated method stub
		return true;
	}

	public boolean eliminarBalance(int idBalance) throws RemoteException {
		// TODO Auto-generated method stub
		return true;
	}

	public boolean eliminarLineaBalance(int idBalance, int idLineaBalance) throws RemoteException {
		boolean resultado = false;
		
		ConexionBD bd = new ConexionBD();
		
		bd.abrir();
		
		if (bd != null)	
			resultado = bd.delete("DELETE FROM LineaBalance WHERE id_linea_balance = " + idLineaBalance);
		
		return resultado;
	}

	public int generarIdFactura() throws RemoteException {
		
		int id = -1;
		ConexionBD bd = new ConexionBD();
		
		bd.abrir();
		
		if (bd != null)
			id = bd.generaId("DatosFactura", "id_ultima_factura");
		
		return id;
	}

	
	
	public Balance getBalance(int id_restaurante, int anio) throws RemoteException {
		// TODO Auto-generated method stub
		ConexionBD bd = new ConexionBD();
		Balance b = null;
		
		bd.abrir();
	
		ResultSet rs = (ResultSet) bd.select("SELECT * FROM BALANCE WHERE id_restaurante="+id_restaurante + " AND anio=" + anio);
		
		try {
			if (rs.next()){
				int id_balance = rs.getInt("id_balance");
				int año = rs.getInt("anio");
				
				b = new Balance(id_balance, año);
			}
		}
		catch(SQLException e){};
		
		rs = (ResultSet) bd.select("SELECT * FROM lineabalance WHERE id_balance=" + b.getIdBalance());
		
		try {
			while(rs.next()){
				int id_linea_balance = rs.getInt("id_linea_balance");
				int total = rs.getInt("cantidad");
				int totalAcumulado = rs.getInt("totalAcumulado");
				int id_producto = rs.getInt("id_producto");
				
				InterfazCarta ic = new AccesoCarta();
				
				Producto p = ic.getProducto(id_producto);
				
				LineaBalance lb = new LineaBalance(id_linea_balance, total, totalAcumulado, p);
				
				b.agregarLineaBalance(lb);
			}
		}
		catch(SQLException e){};

		return b;
	}

	public boolean insertarLineaBalance(int idBalance, LineaBalance lb) throws RemoteException {
		
		ConexionBD bd = new ConexionBD();
		
		bd.abrir();
		
		boolean insertada=false;
		if(bd == null)
			return false;
		
		insertada=bd.insert("INSERT INTO LineaBalance VALUES("+ lb.getIdLineaBalance() +","+lb.getProducto().consultarIdProducto()+","+
				idBalance+","+lb.getCantidad()+",'"+lb.getTotalAcumulado()+"');");
		
		return insertada;

	}

	public List<Balance> getBalances(int id_restaurante) throws RemoteException {
		// TODO Auto-generated method stub
		
		ConexionBD bd = new ConexionBD();
		
		bd.abrir();
		
		List<Balance> lb = new ArrayList<Balance>();
		
		
		ResultSet rs = (ResultSet) bd.select("SELECT * FROM BALANCE WHERE id_restaurante="+id_restaurante);
		
		try {
			while (rs.next()){
				lb.add(getBalance( rs.getInt("id_balance"), rs.getInt("anio") ));
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		return lb;
	}
}
